(function () {
    'use strict';
    /**
     *  @ngdoc overview
     *  @name nogyeadmin.childNav
     *  @description
     *  一个自定义的childNav
     */
    var module = angular.module('nogyeadmin.childNav', []);

    module.directive('childNav', function () {
        return {
            restrict: 'E',
            templateUrl: "./partials/childNav.html",
            replace: true,
            link: function (scope, element, attrs) {
                scope.listGroupShow = true;
                $('#close').bind('click', function (e) {
                    scope.listGroupShow = !scope.listGroupShow;
                    if (!scope.listGroupShow) {
                        $('#sub-nav').addClass('hidden');
                        $('#main-view').removeClass('col-md-10');
                        $('#main-view').addClass('col-md-12');
                        $('#close').removeClass('glyphicon-chevron-left');
                        $('#close').addClass('glyphicon-chevron-right');
                    } else {
                        $('#main-view').removeClass('col-md-12');
                        $('#main-view').addClass('col-md-10');
                        $('#sub-nav').removeClass('hidden');
                        $('#close').removeClass('glyphicon-chevron-right');
                        $('#close').addClass('glyphicon-chevron-left');
                    }
                })
            }
        };
    });

    module.directive('loadingModel', function () {
        return {
            restrict: 'E',
            template: "<div id='loadingModule' class='loading-container loading-hide'><div class='throbber-loader'> Loading… </div></div>",
            scope: {
                control: '='
            },
            replace: true,
            link: function (scope, element, attrs) {
                scope.internalControl = scope.control || {};
                scope.internalControl.showLoading = function () {
                    $("#loadingModule").removeClass('loading-hide');
                };
                scope.internalControl.hideLoading = function () {
                    $("#loadingModule").addClass('loading-hide');
                };
            }
        };
    });


    module.directive('dropdownDate', function () {
        return {
            restrict: 'E',
            templateUrl: "./partials/dropdownDate.html",
            scope: {
                datemodel: '=',
                dataid: "@",
                label: "@"
            },
            replace: true,
            link: function (scope, element, attrs) {

            }
        };
    });

    module.directive('dynamic', function ($compile) {
        return {
            restrict: 'A',
            replace: true,
            link: function (scope, ele, attrs) {
                scope.$watch(attrs.dynamic, function (html) {
                    ele.html(html);
                    $compile(ele.contents())(scope);
                });
            }
        };
    });

    module.directive('contentTable', function () {
        return {
            restrict: 'E',
            templateUrl: "./partials/tool/itemSku.html",
            scope: {
                skuList: '=',
                forestPropertyValueList: '=',
                skuStockChange: '&',
                skuImageChange: '&'
            },
            replace: true,
            link: function (scope, element, attrs) {
                var a = 1;
            }
        };
    });

    module.directive('activityDetailTable', function () {
        return {
            restrict: 'E',
            templateUrl: "./partials/tool/activityDetailTable.html",
            scope: {
                extendList: '=',
                deleteExtend: '&'
            },
            replace: true,
            link: function (scope, element, attrs) {
                var a = 1;
            }
        };
    });

    module.directive('ngModelOnblur', function () {
        return {
            restrict: 'A',
            require: 'ngModel',
            priority: 1, // needed for angular 1.2.x
            link: function (scope, elm, attr, ngModelCtrl) {
                if (attr.type === 'radio' || attr.type === 'checkbox') return;

                elm.unbind('input').unbind('keydown').unbind('change');
                elm.bind('blur', function () {
                    scope.$apply(function () {
                        ngModelCtrl.$setViewValue(elm.val());
                    });
                });
            }
        };
    });


    module.directive('dateDirective', function () {
        return {
            require: 'ngModel',
            link: function (scope, element, attrs, ngModelController) {
                ngModelController.$parsers.push(function (data) {
                    //convert data from view format to model format
                    return data; //converted
                });

                ngModelController.$formatters.push(function (data) {
                    //convert data from model format to view format
                    if (data) {
                        return Food.utils.formatDate(data, "yyyy-MM-dd HH:mm:ss"); //converted
                    }
                    return "";

                });
            }
        }
    });

})();

//页面初始化函数
var pageInit = function () {
    var pathName = window.location.pathname;
    var path = pathName.slice(pathName.lastIndexOf("/") + 1).split(".")[0];
    angular.element("#nav-" + path).addClass("active");
};